<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<el-row :gutter="20">
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_PersonId_FormItem" prop="Entity.PersonId">
							<template #label>
								<WtmLabelTooltip label="员工" tooltip="打卡人员" />
							</template>
							<el-select v-model="stateData.vmModel.Entity.PersonId" clearable class="width100">
								<el-option v-for="item in stateData.AllSysPersons" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
							></el-select>
						</el-form-item>
					</el-col>
				</el-row>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>员工信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_StaffNo_FormItem" prop="Entity.StaffNo">
								<template #label>
									<WtmLabelTooltip label="工号" tooltip="员工工号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.StaffNo" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>时间信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_PunchTime_FormItem"
								prop="Entity.PunchTime"
								:rules="[{ required: true, message: '打卡时间为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="打卡时间" tooltip="实际的打卡时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.PunchTime" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>设备信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_DeviceId_FormItem" prop="Entity.DeviceId">
								<template #label>
									<WtmLabelTooltip label="设备号" tooltip="考勤机的设备编号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.DeviceId" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_DeviceName_FormItem" prop="Entity.DeviceName">
								<template #label>
									<WtmLabelTooltip label="设备名称" tooltip="考勤机的设备名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.DeviceName" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>位置信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Location_FormItem" prop="Entity.Location">
								<template #label>
									<WtmLabelTooltip label="打卡地点" tooltip="打卡的地理位置或区域" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Location" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Longitude_FormItem" prop="Entity.Longitude">
								<template #label>
									<WtmLabelTooltip label="经度" tooltip="打卡位置的经度" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.Longitude" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Latitude_FormItem" prop="Entity.Latitude">
								<template #label>
									<WtmLabelTooltip label="纬度" tooltip="打卡位置的纬度" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.Latitude" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>打卡信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PunchType_FormItem" prop="Entity.PunchType">
								<template #label>
									<WtmLabelTooltip label="打卡类型" tooltip="打卡类型：上班打卡、下班打卡、中途外出等" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.PunchType" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_RecognitionType_FormItem" prop="Entity.RecognitionType">
								<template #label>
									<WtmLabelTooltip label="识别方式" tooltip="识别方式：指纹、人脸、刷卡等" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.RecognitionType" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>技术信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_RawData_FormItem" prop="Entity.RawData">
								<template #label>
									<WtmLabelTooltip label="原始数据" tooltip="考勤机推送的原始JSON数据" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.RawData" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>处理信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ProcessStatus_FormItem" prop="Entity.ProcessStatus">
								<template #label>
									<WtmLabelTooltip label="处理状态" tooltip="记录处理状态：未处理、已处理、处理失败等" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ProcessStatus" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ProcessTime_FormItem" prop="Entity.ProcessTime">
								<template #label>
									<WtmLabelTooltip label="处理时间" tooltip="记录被处理的时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.ProcessTime" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>其他信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Remark_FormItem" prop="Entity.Remark">
								<template #label>
									<WtmLabelTooltip label="备注" tooltip="打卡记录的备注信息" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Remark" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysAttendanceRawRecordApi as api } from '/@/views/hr/sysattendancerawrecord/api';
import other from '/@/utils/other';
import { ISysAttendanceRawRecordVm } from '/@/views/hr/sysattendancerawrecord/api/interface';
import { IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysAttendanceRawRecordVm>>({
	vmModel: {
		Entity: {
			ID: null,
			PersonId: null,
			StaffNo: null,
			PunchTime: new Date(),
			DeviceId: null,
			DeviceName: null,
			Location: null,
			Longitude: null,
			Latitude: null,
			PunchType: null,
			RecognitionType: null,
			RawData: null,
			ProcessStatus: null,
			ProcessTime: null,
			Remark: null,
		},

		ActionName: null,
		IsMyApprove: null,
	},
	AllSysPersons: [],
});

const handleApiResponse = (response: any, isSubmit = false) => {
	ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '提交成功');
	emit('refresh');
	loading.value = false;
	closeDialog();
};
// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 公共的API请求处理函数
const handleApiRequest = (apiCall: () => Promise<any>, beforeSubmit: () => void, isSubmit = false) => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			beforeSubmit();
			apiCall()
				.then((response: any) => handleApiResponse(response, isSubmit))
				.catch(handleError);
		}
	});
};

// 取消
const onClose = () => {
	closeDialog();
};

// 保存
const onSave = () => {
	handleApiRequest(
		() => api().edit(stateData.vmModel),
		() => {},
		false
	);
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		other.getSelectList('/api/SysAttendanceRawRecord/GetSysPersons', [], false).then((x) => (stateData.AllSysPersons = x)),
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.SUBMIT,
			type: 'primary',
			onClick: onSave,
			visible: true,
		},
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
